package fpt.edu.vn.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import fpt.edu.vn.dao.TopicsDao;
import fpt.edu.vn.model.Districts;
import fpt.edu.vn.model.Provinces;
import fpt.edu.vn.util.FuzzyLogic;
import fpt.edu.vn.util.SolrQuerying;

/* 
 * FuzzySearchAction.java 
 *  
 * 0.1
 * 
 * 10/15/2012 
 *  
 * Copyright notice 
 *  
 * Modification Logs: 
 *   DATE        AUTHOR      DESCRIPTION 
 *  -------------------------------------------------------- 
 *  15-Oct-2012  ThuongND    Create new 
 */
public class FuzzySearchAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<Object> mostView;
	// Các tin đã xem
	private List<Object> recentView;

	private List<Provinces> provinces;
	private List<Districts> districts;
	private int sortby;
	private TopicsDao dao = new TopicsDao();
	private float money;
	private int numberPerson;
	FuzzyLogic fuzzylogic = new FuzzyLogic();
	private SolrQuerying solQ = new SolrQuerying();
	private List<Map<String, Object>> lstTopic = new ArrayList<Map<String, Object>>();

	// For SessionAware
	Map<String, Object> session;

	public String execute() {
		this.provinces = dao.listProvinces();
		this.districts = dao.listDistricts();
		this.mostView = dao.listMostView();
		String ipaddress = "";
		session = ActionContext.getContext().getSession();
		if (session.containsKey("ipaddress")) {
			ipaddress = (String) session.get("ipaddress");
		}

		this.recentView = dao.listRecentView(ipaddress);

		String[] fuzzification;
		fuzzification = fuzzylogic.fuzzification(money, numberPerson);
		double[] output;
		output = fuzzylogic.rule_defuzzification(fuzzification[0],
				fuzzification[1], fuzzification[2]);

		if (money == 1) {
			output[0] = 0.8;
			output[1] = 1.2;
		}
		if (money == 2) {
			output[0] = 1.8;
			output[1] = 2.2;
		}
		if (money == 3.5) {
			output[0] = 3.3;
			output[1] = 3.7;
		}
		if (money == 5) {
			output[0] = 4.8;
			output[1] = 5.2;
		}
		if (money > 5) {
			output[0] = 5;
			output[1] = 1.2 * money;
		}
		System.out.println(output[0]);
		System.out.println(output[1]);
		System.out.println(output[2]);
		try {
			lstTopic = solQ.TopicSearchFuzzy(output[0], output[1], output[2],sortby);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public List<Map<String, Object>> getLstTopic() {
		return lstTopic;
	}
	
	public void setLstTopic(List<Map<String, Object>> lstTopic) {
		this.lstTopic = lstTopic;
	}
	
	public int getSortby() {
		return sortby;
	}

	public void setSortby(int sortby) {
		this.sortby = sortby;
	}

	public float getMoney() {
		return money;
	}

	public void setMoney(float money) {
		this.money = money;
	}

	public int getNumberPerson() {
		return numberPerson;
	}

	public void setNumberPerson(int numberPerson) {
		this.numberPerson = numberPerson;
	}

	public List<Object> getMostView() {
		return mostView;
	}

	public void setMostView(List<Object> mostView) {
		this.mostView = mostView;
	}

	public List<Object> getRecentView() {
		return recentView;
	}

	public void setRecentView(List<Object> recentView) {
		this.recentView = recentView;
	}

	public List<Provinces> getProvinces() {
		return provinces;
	}

	public void setProvinces(List<Provinces> provinces) {
		this.provinces = provinces;
	}

	public List<Districts> getDistricts() {
		return districts;
	}

	public void setDistricts(List<Districts> districts) {
		this.districts = districts;
	}

}
